
var wishService = require('../../services/weixin/wishService');
var messageUtil = require('../../common/utils/messageUtil');
//var toolUtil = require('../common/utils/toolUtil');

Date.prototype.addHours = function(h) {
    this.setTime(this.getTime() + (h*60*60*1000));
    return this;
};

var hourDiff = 8;

function addHours(data, hours){
    var hrs = hours ? hours : hourDiff;
    for(var k in data){           // adding hours
        if(data[k] instanceof Date){
            data[k].addHours(hrs);
        }
    }
}

function serviceFunc(req,res,fnName,err,result,code){
    code = code == null ? 1000: code;
    if(result instanceof Array){
        result.forEach(function(r){
            addHours(r,hourDiff);
        })
    }else{
        addHours(result,hourDiff);
    }
    if (err) {
        logger.error('Failed to ' + fnName + '...!' + err.message);
        res.json(messageUtil.composeFailedMessage(req, err.message, code));
    } else {
        res.json(messageUtil.composeSucceededMessage(req, null, result, code));
    }
}

exports.getUserWishList = function (req, res) {
    var wechatId = req.params.wechatId;
    wishService.getUserWishListService(wechatId, function (err, result) {
        serviceFunc(req,res,'getUserWishListService',err,result);
    })
};

exports.getUserSupportedWishList = function (req, res) {
    var wechatId = req.params.wechatId;
    wishService.getUserSupportedWishListService(wechatId, function (err, result) {
        serviceFunc(req,res,'getUserSupportedWishListService',err,result);
    })
};

exports.getJingxuanWishList = function (req, res) {
    wishService.getJingxuanWishListService(function (err, result) {
        serviceFunc(req,res,'getJingxuanWishListService',err,result);
    })
};

exports.getWish = function (req, res) {
    var wishId = req.params.wishId;
    wishService.getWishService(wishId, function (err, result) {
        serviceFunc(req,res,'getWishService',err,result);
    })
};

exports.getWishDetail = function (req, res) {
    var wishId = req.params.wishId;
    wishService.getWishDetailService(wishId, function (err, result) {
        serviceFunc(req,res,'getWishDetailService',err,result);
    })
};

exports.getRandomWish = function (req, res) {
    var wxuser = req.params.wechatId;
    wishService.getRandomWishService(wxuser, function (err, result) {
        serviceFunc(req,res,'getRandomWishService',err,result);
    })
};

exports.makeWish = function (req, res) {
    var user = req.params.wechatId;
    var wishId = req.params.wishId;
    wishService.makeWishService(wishId, user, function (err, result) {
        serviceFunc(req,res,'makeWishService',err,result);
    })
};

//exports.updateWish = function (req, res) {
//    var wishId = req.params.wishId;
//    var input = req.body;
//    wishService.updateWishService(wishId, input, function (err, result) {
//        serviceMessage(req,res,'updateWishService',err,result);
//    })
//};
//
//exports.removeWish = function (req, res) {
//    var wishId = req.params.wishId;
//    wishService.removeWishService(wishId, function (err, result) {
//        serviceMessage(req,res,'removeWishService',err,result);
//    })
//};
//
//exports.solvedWish = function (req, res) {
//    var wishId = req.params.wishId;
//    wishService.solvedWishService(wishId, function (err, result) {
//        serviceFunc(req,res,'solvedWishService',err,result);
//    })
//};

exports.addWishSupporter = function (req, res) {
    var wishId = req.params.wishId;
    var supporter = req.params.wechatId;
    wishService.addWishSupporterService(supporter, wishId, function (err, result) {
        serviceFunc(req,res,'addWishSupporterService',err,result);
    })
};

exports.removeWishSupporter = function (req, res) {
    var wishId = req.params.wishId;
    var supporter = req.params.wechatId;
    wishService.removeWishSupporterService(supporter, wishId, function (err, result) {
        serviceFunc(req,res,'removeWishSupporterService',err,result);
    })
};
